package com.phuc.game.tankshooter.sprite;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.PathModifier;
import org.anddev.andengine.entity.modifier.PathModifier.IPathModifierListener;
import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.constants.TimeConstants;

import android.util.Log;

import com.phuc.game.tankshooter.MainActivity;

public class RocketSprite extends AnimatedSprite implements TimeConstants {

	public static final int WIDTH = 20;
	public static final int HEIGHT = 35;
	private static final float MAX_DURATION = 0.7f;
	private static final int RANGE_ATTACK = 60;
	private static final int DAME_MAX = 60;
	
	private Path path;
	Scene scene;
	boolean isRemoved;

	public RocketSprite(float pX, float pY, float pTileWidth, float pTileHeight,
			TiledTextureRegion pTiledTextureRegion, Scene scene) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);

		this.scene = scene;
		isRemoved = false;
		settingMovePath((int) pX, (int) pY);
		MainActivity.getInstant().getGamePlay().getGun().setRotation(this.getRotation());
	}

	public void settingMovePath(int pX, int pY) {
		int x1 = MainActivity.SCENE_WIDTH / 2 - WIDTH / 2;
		int y1 = MainActivity.SCENE_HEIGH - 80;
		final int x2 = pX;
		final int y2 = pY;
		path = new Path(2).to(x1, y1).to(x2, y2);
		
		float duration = MAX_DURATION * ((float)(MainActivity.SCENE_HEIGH - pY) / (float)MainActivity.SCENE_HEIGH);
		double rotation = Math.atan(((double)pX - (double)MainActivity.SCENE_WIDTH / 2) / ((double)MainActivity.SCENE_HEIGH - (double)pY));
		double rotationDeg = Math.toDegrees(rotation);
		this.setRotation((float)rotationDeg);

		PathModifier pm = new PathModifier(duration, path, null,
				new IPathModifierListener() {

					@Override
					public void onPathStarted(PathModifier pPathModifier,
							IEntity pEntity) {

					}

					@Override
					public void onPathWaypointStarted(
							PathModifier pPathModifier, IEntity pEntity,
							int pWaypointIndex) {
					}

					@Override
					public void onPathWaypointFinished(
							PathModifier pPathModifier, IEntity pEntity,
							int pWaypointIndex) {
						Log.d("phucphuc", "phucphuc onPathWaypointFinished");
					}

					@Override
					public void onPathFinished(PathModifier pPathModifier,
							final IEntity pEntity) {
						MainActivity.getInstant().runFromUpdateThread(new Runnable() {
							@Override
							public void run() {
								MainActivity.getInstant().getGamePlay().createExplode(
										x2 + WIDTH / 2, y2);
								
								// Attack enemy on map
								for (int i = 0; i < EnemyObjectSprite.tankEnemy.size(); i++) {
									EnemyObjectSprite tank = EnemyObjectSprite.tankEnemy.get(i);
									float tankLocationX = tank.getX() + tank.getTankSizeW() / 2;
									float tankLocationY = tank.getY() + tank.getTankSizeH() / 2;
									
									float rX = Math.abs(x2 + WIDTH / 2 - tankLocationX);
									float rY = Math.abs(y2 + HEIGHT / 2 - tankLocationY);
									
									float rangeBetweenRocketAndObject = (float) Math.sqrt((rX * rX + rY * rY));
									float maxRangeBetweenCenter = RANGE_ATTACK + tank.getSize();
									
									// Trong truong hop center cua doi tuong nam trong attack area
									if (rangeBetweenRocketAndObject < RANGE_ATTACK && rangeBetweenRocketAndObject >= 2 * RANGE_ATTACK / 3) {
										tank.attacked(DAME_MAX / 4);
									} else if (rangeBetweenRocketAndObject < 2 * RANGE_ATTACK / 3 && rangeBetweenRocketAndObject >= RANGE_ATTACK / 3) {
										tank.attacked(DAME_MAX / 2);
									} else if (rangeBetweenRocketAndObject > 0 && rangeBetweenRocketAndObject < RANGE_ATTACK / 3) {
										tank.attacked(DAME_MAX);
									}
									// Trong truong hop center cua doi tuong nam ngoai attack area
									else if (rangeBetweenRocketAndObject >= 2 * maxRangeBetweenCenter / 3
											&& rangeBetweenRocketAndObject < maxRangeBetweenCenter) {
										tank.attacked(DAME_MAX / 10);
									} else if (rangeBetweenRocketAndObject >= maxRangeBetweenCenter / 3
											&& rangeBetweenRocketAndObject < 2 * maxRangeBetweenCenter / 3) {
										tank.attacked(DAME_MAX / 8);
									} else if (rangeBetweenRocketAndObject >= 0
											&& rangeBetweenRocketAndObject < maxRangeBetweenCenter / 3) {
										tank.attacked(DAME_MAX / 5);
									}
								}
								
								// Attack item
								for (int i = 0; i < ItemSprite.listItemSprites.size(); i++) {
									ItemSprite item = ItemSprite.listItemSprites.get(i);
									item.attacked(RocketSprite.this.getX() + WIDTH / 2,
											RocketSprite.this.getY() + HEIGHT / 2, RANGE_ATTACK);
								}
								
								pEntity.detachSelf();
								pEntity.setIgnoreUpdate(true);
								pEntity.clearEntityModifiers();
								pEntity.clearUpdateHandlers();
							}
						});
					}
				});
		this.registerEntityModifier(new ParallelEntityModifier(pm));
		
//		IEntityModifier i;
	}
	
	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
	}
}
